#-*-coding:utf-8-*-
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
import qrcode
import base64
import random
import string
from cStringIO import StringIO
tokenLen = 20
token = ''


def index(request):
    global token
    token = ''.join(random.sample(string.ascii_letters, tokenLen))
    response = HttpResponse()

    # 这个可以玩一下
    #img = qrcode.make("https://www.baidu.com/s?wd=access_token")

    # 上线了就用这个
    #img = qrcode.make("http://turing.ymyin.me/CheckIn/"+token)
    img = qrcode.make("http://59.77.42.227/"+token)
    buf = StringIO()
    img.save(buf)
    data = base64.b64encode(buf.getvalue())
    return render(request, 'app_CheckIn/home.html', {"token": token, "imgData": data})


def check(request):
    response = HttpResponse()
    if token not in request.path_info:
        response.write("抱歉，页面已过期")
    else:
        response.write("签到成功！")
        # do sth else
    return response
